package com.navinfo.platform.trip.analysis.service.impl.qingqi;

import com.navinfo.platform.trip.analysis.pojo.TripJudgeState;
import com.navinfo.platform.trip.common.annotation.SystemType;
import com.navinfo.platform.trip.common.config.ConfigMap;
import com.navinfo.platform.trip.common.enums.SystemTypeEnum;
import com.navinfo.platform.trip.common.enums.TripStatusEnum;
import com.navinfo.platform.trip.analysis.service.TripJudgeService;
import com.navinfo.platform.trip.common.pojo.PointProtocol;
import org.apache.flink.api.java.utils.ParameterTool;

import java.util.Optional;

/**
 * 青汽行程切分服务
 * 行程第一个点：转速>500，行程开始；否则，行程未开始；
 * 行程非第一个点：
 *      两点时差大于10分钟：行程强制结束；
 *      两点时差小于10分钟：转速<=500且连续3秒转速小于500转每分钟），行程结束；否则，行程进行中；
 *
 * @author: web
 * @data:  2020-04-07
 **/
@SystemType(value= SystemTypeEnum.QQ, interfaceClass= TripJudgeService.class )
public class TripJudgeServiceImpl implements TripJudgeService {
    /**
     * 行程切分转速阀值
     */
    private final int ROTATION_THRESHOLD = 500;
    /**
     * 行程结束发动机转速小于500的连续时长阀值，单位：ms
     */
    private final int DURATION_THRESHOLD = 3000;

    /**
     * 最小速度阀值
     */
    private int speedMin, speedMax;
    /**
     * 引擎最小转速阀值
     */
    private int rotationMin, rotationMax;

    public TripJudgeServiceImpl(ParameterTool jobConfig) {
        rotationMin = jobConfig.getInt("rotation_min", 0);
        rotationMax = jobConfig.getInt("rotation_max", 10000);
        speedMin = jobConfig.getInt("speed_min", 0);
        speedMax = jobConfig.getInt("speed_max", 200);
    }

    @Override
    public SystemTypeEnum systemType() {
        return SystemTypeEnum.QQ;
    }

    @Override
    public TripStatusEnum tripStatus(TripJudgeState tripJudgeState, PointProtocol point) {
        //行程未开始
        if (null==tripJudgeState || null==tripJudgeState.getLastGpsTime() ) {
            return point.getRotation()>ROTATION_THRESHOLD ? TripStatusEnum.TRIP_START : TripStatusEnum.TRIP_NO_START;
        }

        //行程已开始
        else {
            long currGpsTime = point.getGpsTime();
            if (isForceEnd(Optional.ofNullable(tripJudgeState.getLastGpsTime()).orElse(currGpsTime), currGpsTime)) {
                return TripStatusEnum.TRIP_FORCE_END;
            } else {
                //结束条件：转速<=500且连续3秒转速小于500转每分钟
                return (point.getRotation() <= ROTATION_THRESHOLD && tripJudgeState.getLowRotationDuration() >= DURATION_THRESHOLD) ? TripStatusEnum.TRIP_END : TripStatusEnum.TRIP_IN;
            }
        }
    }

    @Override
    public boolean validate(PointProtocol point, long lastGpsTime) {
        //本次gps时间小于等于上个点的gps时间，不予处理!
        if (point.getGpsTime() <= lastGpsTime) {
            return false;
        }

        //发动机转速不在合理区间内
        int rotation = point.getRotation();
        if (rotation<rotationMin || rotation>rotationMax) {
            return false;
        }

        //速度不在合理区间内
        double speedKmh = point.getSpeedKmh();
        if(speedKmh<speedMin || speedMax>200) {
            return false;
        }

        return true;
    }

}
